import NProgress from "nprogress";
import { createRouter, createWebHistory } from "vue-router";

// 导入路由组件
const routes = [
  {
    path: "/",
    name: "login",
    // 懒加载组件
    component: () => import("../login/index.vue"),
    meta: {
      title: "登录",
      icon: "",
    },
  },
  {
    path: "/home",
    name: "home",
    component: () => import("../layout/index.vue"),
    meta: {
      title: "首页",
      icon: "HomeFilled",
    },
    children: [
      {
        path: "/product/trademark",
        name: "trademark",
        component: () => import("../views/product/trademark/index.vue"),
        meta: {
          title: "品牌管理",
          icon: "Shop",
        },
      },
      {
        path: "/product/attr",
        name: "attr",
        component: () => import("../views/product/arrt/index.vue"),
        meta: {
          title: "属性管理",
          icon: "Shop",
        },
      },
      {
        path: "/product/spu",
        name: "spu",
        component: () => import("../views/product/spu商品/index.vue"),
        meta: {
          title: "spu商品",
          icon: "Shop",
        },
      },
      {
        path: "/product/sku",
        name: "sku",
        component: () => import("../views/product/sku商品/index.vue"),
        meta: {
          title: "sku商品",
          icon: "Shop",
        },
      },
      {
        path: "/user/list",
        name: "list",
        component: () => import("../views/user/userUser/index.vue"),
        meta: {
          title: "用户管理",
          icon: "Shop",
        },
      },
      {
        path: "/user/role",
        name: "userList",
        component: () => import("../views/user/userList/index.vue"),
        meta: {
          title: "用户列表",
          icon: "Shop",
        },
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});
router.beforeEach((to, from, next) => { 
    let token = localStorage.getItem("token");
    if(token){
      next();
    }else{
      if(to.path === '/'){
        next();
      }else{
        next('/')
      }

    }
});

export default router;
